package com.escom.service;

import android.support.v4.app.FragmentActivity;

import com.escom.healthlc.entidad.OximetroORM;
import com.escom.utils.Constantes;
import com.escom.utils.CustomOximetroAdapter;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import java.util.StringTokenizer;

/**
 * Created by Carlos on 20/04/2014.
 */
public class OximetroService implements Runnable {

    private OximetroObservable oximetroObservable;
    private CustomOximetroAdapter customOximetroAdapter;
    private FragmentActivity fragmentActivity;
    List<OximetroORM> oximetroList ;


    public OximetroService(CustomOximetroAdapter customOximetroAdapter, FragmentActivity fragmentActivity, List<OximetroORM> oximetroList ) {
        this.customOximetroAdapter = customOximetroAdapter;
        this.fragmentActivity = fragmentActivity;
        this.oximetroList = oximetroList;
        oximetroObservable = new OximetroObservable();
    }

    class OximetroObservable extends Observable {
        @Override
        public void notifyObservers() {
            setChanged();
            super.notifyObservers();
        }
    }

    @Override
    public void run() {
        try {
            byte buf[] = new byte[32];
            int i = 0;
            MulticastSocket s = new MulticastSocket(Constantes.PORT_OXIMETRO);
            s.joinGroup(InetAddress.getByName(Constantes.GROUP));
            DatagramPacket pack = new DatagramPacket(buf, buf.length);
            while (true) {
                s.receive(pack);
                byte[] data = pack.getData();
                String valueByte = new String(data);
                StringTokenizer stringTokenizer = new StringTokenizer(valueByte, ",");
                String bpm = stringTokenizer.nextToken();
                String oxygenSaturation = stringTokenizer.nextToken();
                OximetroORM oximetro = new OximetroORM();
                oximetro.setBpm(bpm);
                oximetro.setOxygenSaturation(oxygenSaturation);
                oximetro.setCreateDate(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(Calendar.getInstance().getTime()));
                oximetroList.add(oximetro);
                customOximetroAdapter.setDrawerItemOximetro(oximetroList);
                fragmentActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        oximetroObservable.notifyObservers();
                    }
                });
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public void addObserver(Observer observer) {
        oximetroObservable.addObserver(observer);
    }

}
